<template>
  <div id="home">
    <div class="topbar">
      <div class="top-left">
        <van-icon name="location-o" size="30" />
        <div class="location">养公佳苑</div>
        <van-icon name="arrow-down" size="16" @click="goAddress" />
      </div>
      <div>
        <img
          src="../../assets/image/home/消息中心@2x.png"
          alt=""
          @click="goNews"
        />
      </div>
    </div>
    <div class="banner">
      <div class="search">
        <van-icon class="searchIcon" name="search" size="20" color="#ffcc00" />
        <input
          class="searchInp"
          type="text"
          placeholder="石榴"
          @keyup.enter="onSearch"
          v-model="searchVal"
        />
        <div class="searchBtn" @click="onSearch">搜索</div>
      </div>
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="i in 4" :key="i">
          <img src="../../assets/image/home/banner.png" alt="" />
        </van-swipe-item>
      </van-swipe>
      <div class="banner-word">
        <span>最快30分钟送达</span>
        <span>满39免配送费</span>
      </div>
    </div>
    <div class="main">
      <div class="service">
        <div v-for="s in service" :key="s.title">
          <img :src="s.img" alt="" @click="goClassify(s.title)" />
          <div class="service-title">{{ s.title }}</div>
        </div>
      </div>
      <div class="sales">
        <div>
          <div class="sales-title">
            <h5>限时抢购</h5>
            <van-count-down
              ref="countDown"
              millisecond
              :time="7200000"
              :auto-start="true"
              format="HH:mm:ss"
              @finish="finish"
            />
          </div>
          <div class="sales-img">
            <img src="../../assets/image/home/肉@2x.png" alt="" />
            <div class="go">GO</div>
          </div>
        </div>
        <div>
          <div class="sales-title">
            <h5>限时抢购</h5>
          </div>
          <div class="sales-img">
            <img src="../../assets/image/home/aaa.png" alt="" />
            <div class="go">GO</div>
          </div>
        </div>
      </div>
      <div class="goods-card">
        <div v-for="g in goodslist" :key="g.id">
          <img :src="g.url" alt="" @click="goDetail(g.id)" />
          <div class="goods-card-title">{{ g.name }}</div>
          <p>{{ g.des }}</p>
          <div class="goods-card-bottom">
            <div class="goods-price">
              ￥<span>{{ parseInt(g.price) }}</span
              >{{ g.price.toFixed(2).slice(-3) }}
            </div>
            <div class="incart">
              <van-icon
                name="cart-circle-o"
                size="24"
                color="#ffcc00"
                @click="inCart(g.id)"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "Home",
  data() {
    return {
      searchVal: "",
      service: [
        {
          title: "蔬菜水果",
          img: require("../../assets/image/home/蔬果@2x.png"),
        },
        {
          title: "肉禽蛋类",
          img: require("../../assets/image/home/肉禽@2x.png"),
        },
        {
          title: "海鲜水产",
          img: require("../../assets/image/home/水产@2x.png"),
        },
        {
          title: "速食冻品",
          img: require("../../assets/image/home/速冻@2x.png"),
        },
        {
          title: "酒水饮料",
          img: require("../../assets/image/home/酒水@2x.png"),
        },
        {
          title: "乳品烘焙",
          img: require("../../assets/image/home/烘培@2x.png"),
        },
        {
          title: "粮油调味",
          img: require("../../assets/image/home/调味@2x.png"),
        },
        {
          title: "休闲零食",
          img: require("../../assets/image/home/零食@2x.png"),
        },
      ],
    };
  },
  computed: {
    ...mapState(["goodslist"]),
  },

  mounted() {
    if (!localStorage.getItem("vuex")) {
      this.$store.dispatch("getGoodslist");
    }
  },
  methods: {
    onSearch() {
      if (this.searchVal) {
        this.$store.commit("inSearch", this.searchVal);
        this.$router.push("/search");
      }
    },
    goDetail(id) {
      this.$router.push({
        path: "/detail",
        query: {
          id: id,
        },
      });
    },
    goNews() {
      this.$router.push({
        name: "xiaoxi",
      });
    },
    goAddress() {
      this.$router.push("/address");
    },
    goClassify(title) {
      this.$router.push({
        path: "/classify",
        query: {
          title: title,
        },
      });
    },
    finish() {
      this.$refs.countDown.reset();
    },
    inCart(id) {
      this.$store.commit("inCart", id);
      this.$toast({
        type: "success",
        message: "加入成功",
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/less/var";
#home {
  width: 100%;
  height: 100%;
  .topbar {
    padding: 0 40px;
    width: 100%;
    height: 88px;
    background-color: @c;
    .flex-sb();
    .top-left {
      display: flex;
      align-items: center;
      color: #fff;
      .location {
        font-size: 40px;
        margin-left: 32px;
        margin-right: 30px;
        font-weight: 400;
        color: @fc;
      }
    }
    img {
      width: 45px;
    }
  }
  .banner {
    padding: 10px 40px;
    height: 495px;
    background-color: @c;
    .search {
      position: relative;
      height: 58px;
      display: flex;
      .searchIcon {
        position: absolute;
        top: 10px;
        left: 24px;
      }
      .searchInp {
        width: 748px;
        height: 58px;
        background: #ffffff;
        border-radius: 29px;
        border: none;
        padding: 0 81px 0 90px;
        font-size: 26px;
      }
      input::placeholder {
        font-size: 26px;
        font-weight: 400;
        color: #d6d6d6;
      }
      .searchBtn {
        position: absolute;
        color: @fc;
        top: 6px;
        right: 10px;
        font-size: 26px;
        width: 101px;
        height: 48px;
        background: @c;
        border-radius: 24px;
        line-height: 48px;
        text-align: center;
      }
    }
    .my-swipe {
      margin-top: 20px;
      height: 343px;
      img {
        display: block;
        width: 100%;
        height: 343px;
        border-radius: 28px;
      }
      ::v-deep .van-swipe__indicator {
        width: 14px;
        height: 14px;
        margin: 18px;
      }
    }
    .banner-word {
      .flex-sb();
      font-size: 26px;
      color: #646464;
      margin-top: 10px;
      font-weight: 400;
    }
  }
  .main {
    height: calc(100% - 88px - 495px - 98px);
    padding: 20px 40px;
    padding-bottom: 100px;
    background-color: #fff;
    .service {
      height: 300px;
      background: @bc;
      border-radius: 28px;
      display: flex;
      flex-wrap: wrap;
      > div {
        width: 25%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        img {
          width: 103px;
          height: 83px;
        }
        .service-title {
          font-size: 22px;
          font-weight: 400;
          color: #444444;
          margin-top: 10px;
        }
      }
    }
    .sales {
      margin-top: 20px;
      display: flex;
      > div {
        width: 364px;
        margin: auto;
        height: 174px;
        background-color: @bc;
        border-radius: 28px;
        padding: 10px 36px;
        font-size: 22px;
        overflow: hidden;
        .sales-title {
          display: flex;
          align-items: center;
          h5 {
            font-size: 26px;
            color: #444444;
            font-weight: 400;
            margin-right: 10px;
          }
        }
        .sales-img {
          position: relative;
          img {
            width: 123px;
            height: 123px;
            margin-top: 5px;
          }
          .go {
            position: absolute;
            right: 20px;
            bottom: 39px;
            width: 58px;
            height: 28px;
            background: #ffcc00;
            border-radius: 14px;
            color: @fc;
            line-height: 28px;
            text-align: center;
          }
        }
      }
    }
    .goods-card {
      display: flex;
      flex-wrap: wrap;
      > div {
        margin-top: 20px;
        width: 364px;
        height: 450px;
        background-color: @bc;
        border-radius: 28px;
        font-size: 22px;
        padding: 0 33px 10px 19px;
        &:nth-child(odd) {
          margin-right: 10px;
        }
        img {
          width: 300px;
          height: 300px;
          display: block;
          margin: auto;
        }
        .goods-card-title {
          color: #444444;
          font-size: 26px;
          width: 310px;
          .line1();
        }
        p {
          width: 310px;
          font-size: 22px;
          color: #909090;
          .line1();
        }
        .goods-card-bottom {
          display: flex;
          justify-content: space-between;
          margin-top: 40px;
          .goods-price {
            color: @pc;
            font-size: 22px;
            span {
              font-size: 26px;
            }
          }
        }
      }
    }
  }
  .van-count-down {
    color: @pc;
  }
}
</style>
